package com.sync.syncmasterdata.product.service.convert;

import com.sync.syncmasterdata.product.entity.wms.EleProduct;
import com.sync.syncmasterdata.product.entity.wms.EleProductUnit;
import com.sync.syncmasterdata.product.pojo.gsp.InventoryForGsp;
import com.sync.syncmasterdata.product.pojo.oa.InventoryForOA;
import com.sync.syncmasterdata.product.repository.u8.ComputationunitRepository;
import com.sync.syncmasterdata.product.repository.wms.ProductUnitForWmsRepository;
import com.sync.syncmasterdata.product.service.ConvertInterface;
import com.sync.syncmasterdata.product.view.StoreTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

import static com.sync.syncmasterdata.util.NormalUtil.null2String;

/**
 * oa 产品对象转 wms产品对象
 */
@Service
@Slf4j
public class ProductOAtoWms implements ConvertInterface<InventoryForOA, InventoryForGsp[]> {

    private String CW = "常温";

    @Autowired
    private ComputationunitRepository computationunitRepository;


    /**
     * 转化为GSP系统产品对象
     * @param oa
     * @return
     */
    @Override
    public InventoryForGsp[] convertTo(InventoryForOA oa){
        //确认是否包含多温度
        Integer tmpFlag = Objects.isNull(oa.getShifoucw()) ? 1 : oa.getShifoucw();
        if (tmpFlag == 2) {
            //多温度 等待前端完成后处理
            String[] names = splitStr(oa.getZixiangmc());
            String[] wenducs = splitStr(oa.getWenduc1());
            String[] wenduzs = splitStr(oa.getWenduz1());
            String[] storeType1 = splitStr(oa.getStoreType1());
            log.info("多温度字段长度names->{},wenducs->{},wenduzs->{},storeType1->{}", names.length, wenducs.length, wenduzs.length, storeType1);
            //数组长度校验
            if (names.length != wenducs.length || wenduzs.length != names.length || wenducs.length != wenduzs.length || names.length != storeType1.length) {
                log.info("多温度校验异常");
                return null;
            } else {
                //校验通过
                log.info("多温度校验成功");
                //先处理mom
                InventoryForGsp[] gspProducts = new InventoryForGsp[names.length + 1];
                InventoryForGsp momGspProduct = new InventoryForGsp();
                momGspProduct.setGoodsCode(oa.getCunhuobm());
                momGspProduct.setGoodsName(oa.getChanpinmc());
                momGspProduct.setArchNo("常温");
                momGspProduct.setMinTemp("");
                momGspProduct.setMaxTemp("");
                momGspProduct.setBrandName(oa.getPinpaiwb());
                momGspProduct.setGoodsRef(oa.getHuohao());
                momGspProduct.setGoodsSpec(oa.getGuigexh());
                momGspProduct.setUnit(oa.getUnitName());
                momGspProduct.setGoodsDesc(oa.getBeizhu());
                if (oa.getShifouylqx() == 0) {
                    //非医疗器械
                    momGspProduct.setGCategory("040101");
                    momGspProduct.setIsFirst("Y");
                } else {
                    momGspProduct.setIsFirst("N");
                }
                if (oa.getYanghulx() == 1) {
                    momGspProduct.setCurCycle(30);
                } else {
                    momGspProduct.setCurCycle(90);
                }
                momGspProduct.setInEffectDay(oa.getYouxiaoq());
                momGspProduct.setBrandName(oa.getPinpaiwb());
                momGspProduct.setWTManufacturer(oa.getShengchanqywb());
                momGspProduct.setStorageTerm(StoreTypeEnum.getValue(oa.getStoreType()));
                gspProducts[0] = momGspProduct;

                for (int i = 0; i < names.length; i++) {
                    InventoryForGsp gspProduct = new InventoryForGsp();
                    gspProduct.setGoodsCode(String.format("%s-%d", oa.getCunhuobm(), (i + 1)));
//                    gspProduct.setGoodsName(names[i]);
                    gspProduct.setGoodsName(String.format("%s-%d", oa.getChanpinmc(), (i + 1)));
                    //多温度的货物类型配置
                    gspProduct.setStorageTerm(StoreTypeEnum.getValue(Integer.valueOf(storeType1[i])));


                    //校验温度的状态，是否常温 ，温度高低
                    if (wenducs[i].contains(CW) && wenduzs[i].contains(CW)) {
                        gspProduct.setArchNo("常温");
                        gspProduct.setMinTemp("");
                        gspProduct.setMaxTemp("");
                    } else {
                        gspProduct.setArchNo("");
                        gspProduct.setMinTemp(null2String(wenducs[i]));
                        gspProduct.setMaxTemp(null2String(wenduzs[i]));
                    }

                    //其余产品配置一致
                    //货号
                    gspProduct.setGoodsRef(oa.getHuohao());
                    //规格
                    gspProduct.setGoodsSpec(oa.getGuigexh());
                    //计价单位需要转换
                    gspProduct.setUnit(oa.getUnitName());
                    //产品备注
                    gspProduct.setGoodsDesc(oa.getBeizhu());
                    //是否已经首营 默认=N
//                    gspProduct.setIsFirst("N");
                    //是否医疗器械,需要转换
                    if (oa.getShifouylqx() == 0) {
                        //非医疗器械
                        gspProduct.setGCategory("040101");
                        gspProduct.setIsFirst("Y");
                    } else {
                        gspProduct.setIsFirst("N");
                    }
                    //增加养护类型 的调整
                    if (oa.getYanghulx() == 1) {
                        gspProduct.setCurCycle(30);
                    } else {
                        gspProduct.setCurCycle(90);
                    }
                    gspProduct.setInEffectDay(oa.getYouxiaoq());
                    gspProduct.setBrandName(oa.getPinpaiwb());
                    gspProduct.setWTManufacturer(oa.getShengchanqywb());
                    gspProducts[i + 1] = gspProduct;
                }
                log.info("多温度返回成功");
                return gspProducts;
            }

        } else {
            //单温度
            InventoryForGsp[] gspProducts = new InventoryForGsp[1];
            InventoryForGsp gspProduct = new InventoryForGsp();

            gspProduct.setGoodsCode(oa.getCunhuobm());
            gspProduct.setGoodsName(oa.getChanpinmc());
            gspProduct.setBrandName(oa.getPinpaiwb());
            //货号
            gspProduct.setGoodsRef(oa.getHuohao());
            //规格
            gspProduct.setGoodsSpec(oa.getGuigexh());
            //计价单位需要转换
            gspProduct.setUnit(oa.getUnitName());
            //备注信息
            gspProduct.setGoodsDesc(oa.getBeizhu());

            //是否医疗器械,需要转换
            if(oa.getShifouylqx() == 0){
                //非医疗器械
                gspProduct.setGCategory("040101");
                gspProduct.setIsFirst("Y");
            }else{
                gspProduct.setIsFirst("N");
            }

            //校验温度的状态，是否常温 ，温度高低
            if (oa.getShifoucw()==0) {
                gspProduct.setArchNo("");
                gspProduct.setMinTemp(null2String(oa.getWenduc()));
                gspProduct.setMaxTemp(null2String(oa.getWenduz()));
            } else {
                gspProduct.setArchNo("常温");
                gspProduct.setMinTemp("");
                gspProduct.setMaxTemp("");
            }

            //增加养护类型 的调整
            if(oa.getYanghulx()==1){
                gspProduct.setCurCycle(30);
            }else{
                gspProduct.setCurCycle(90);
            }
            gspProduct.setInEffectDay(oa.getYouxiaoq());
            gspProduct.setBrandName(oa.getPinpaiwb());
            gspProduct.setWTManufacturer(oa.getShengchanqywb());
            //新增 货物类型
            gspProduct.setStorageTerm(StoreTypeEnum.getValue(oa.getStoreType()));
            gspProducts[0] = gspProduct;
            return gspProducts;
        }
    }
//
//    /**
//     * 转化为wms系统的产品对象
//     * @param oa
//     * @return
//     */
//    @Override
//    public EleProduct[] convertTo(InventoryForOA oa) {
//        //确认是否包含多温度
//        Integer tmpFlag = Objects.isNull(oa.getShifoucw()) ? 1 : oa.getShifoucw();
//        if (tmpFlag == 2) {
//            //多温度 等待前端完成后处理
//            String[] names = splitStr(oa.getZixiangmc());
//            String[] wenducs = splitStr(oa.getWenduc1());
//            String[] wenduzs = splitStr(oa.getWenduz1());
//            log.info("多温度字段长度names-{},wenducs-{},wenduzs-{}",names.length,wenducs.length,wenduzs.length);
//            //数组长度校验
//            if (names.length != wenducs.length || wenduzs.length != names.length || wenducs.length != wenduzs.length) {
//                log.info("多温度校验异常");
//                return null;
//            } else {
//                //校验通过
//                log.info("多温度校验成功");
//                EleProduct[] eleProducts = new EleProduct[names.length];
//                for (int i = 0; i < names.length; i++) {
//                    EleProduct eleProduct = new EleProduct();
//                    eleProduct.setSCode(String.format("%s-%d", oa.getCunhuobm(), (i + 1)));
//                    eleProduct.setSName(names[i]);
//                    //校验温度的状态，是否常温 ，温度高低
//                    if (wenducs[i].contains(CW) && wenduzs[i].contains(CW)) {
//                        eleProduct.setBNormalTpr(1);
//                        eleProduct.setDTemperatureFrm(strToDouble(null));
//                        eleProduct.setDTemperatureTo(strToDouble(null));
//                    } else {
//                        eleProduct.setBNormalTpr(0);
//                        eleProduct.setDTemperatureFrm(strToDouble(wenducs[i]));
//                        eleProduct.setDTemperatureTo(strToDouble(wenduzs[i]));
//                    }
//
//                    //其余产品配置一致
//                    //分类
//                    eleProduct.setSProductClass(oa.getChanpinlx());
//                    //货号
//                    eleProduct.setSProductModel(oa.getHuohao());
//                    //规格
//                    eleProduct.setSProductStyle(oa.getGuigexh());
//                    //计价单位需要转换
//                    String unitName = danweizhuanhuan(oa.getUnitName());
//                    eleProduct.setSValuationUnit(unitName);
//                    eleProduct.setSBasicUnit(unitName);
//                    //有效预警 天
//                    eleProduct.setDStoreDay(oa.getYujingyxq());
//                    eleProduct.setBUnActive(0);
//                    eleProduct.setSMemo(oa.getBeizhu());
//                    eleProduct.setSCrtUser("admin");
//                    eleProduct.setDUnitConvertRate(1.00);
//                    eleProduct.setBStopSale(shifoutingshou(oa.getTingshou()));
//
//                    //是否已经首营 默认=0
//                    eleProduct.setBFirstPass("0");
//
//                    //是否医疗器械,需要转换
//                    eleProduct.setBNotMedical(convMedical(oa.getShifouylqx()));
//                    //增加养护类型
//                    eleProduct.setSProductMaintanceType(String.valueOf(oa.getYanghulx()));
//                    eleProduct.setBOneBarCode(0);
//                    eleProduct.setDEffectDays(oa.getYouxiaoq());
//                    eleProduct.setSEffectType("2");
//                    eleProduct.setSBrand(oa.getPinpaiwb());
//                    eleProduct.setSFactoryName(oa.getShengchanqywb());
//                    eleProduct.setBDeviceID(0);
//                    //新增(默认null)
//                    eleProduct.setSFuncDesc(null);
//                    eleProduct.setSComposition(null);
//                    eleProducts[i] = eleProduct;
//                }
//                log.info("多温度返回成功");
//                return eleProducts;
//            }
//
//        } else {
//            //单温度
//            EleProduct[] products = new EleProduct[1];
//            EleProduct product = new EleProduct();
//
//            product.setSCode(oa.getCunhuobm());
//            product.setSName(oa.getChanpinmc());
//            //分类
//            product.setSProductClass(oa.getChanpinlx());
//            //货号
//            product.setSProductModel(oa.getHuohao());
//            //规格
//            product.setSProductStyle(oa.getGuigexh());
//            //计价单位需要转换
//            String unitName = danweizhuanhuan(oa.getUnitName());
//            product.setSValuationUnit(unitName);
//            product.setSBasicUnit(unitName);
//            //有效预警 天
//            product.setDStoreDay(oa.getYujingyxq());
//            product.setBUnActive(0);
//            product.setSMemo(oa.getBeizhu());
//            product.setSCrtUser("admin");
//            product.setDUnitConvertRate(1.00);
//            product.setBStopSale(shifoutingshou(oa.getTingshou()));
//            //是否常温
//            product.setBNormalTpr(shifoutingshou(oa.getShifoucw()));
//            //温度相关
//            product.setDTemperatureFrm(oa.getWenduc());
//            product.setDTemperatureTo(oa.getWenduz());
//
//            //是否已经首营 默认=0
//            product.setBFirstPass("0");
//
//            //是否医疗器械,需要转换
//            product.setBNotMedical(convMedical(oa.getShifouylqx()));
//            //增加养护类型
//            product.setSProductMaintanceType(String.valueOf(oa.getYanghulx()));
//            product.setBOneBarCode(0);
//            product.setDEffectDays(oa.getYouxiaoq());
//            product.setSEffectType("2");
//            product.setSBrand(oa.getPinpaiwb());
//            product.setSFactoryName(oa.getShengchanqywb());
//            product.setBDeviceID(0);
//            //新增(默认null)
//            product.setSFuncDesc(null);
//            product.setSComposition(null);
//            products[0] = product;
//            return products;
//        }
//    }

    /**
     * 是否停售转换
     *
     * @param integer
     * @return
     */
    private int shifoutingshou(Integer integer) {
        try {
            if (integer == null) {
                return 0;
            } else {
                if (integer != 0) {
                    return 1;
                } else {
                    return 0;
                }
            }
        } catch (Exception e) {
            return 0;
        }

    }

    /**
     * 是否医疗器械的转换
     */
    @Deprecated
    private Integer convMedical(Integer integer) {
        try {
            if (integer == null) {
                return 1;
            } else {
                if (integer == 0) {
                    return 1;
                } else {
                    return 0;
                }
            }
        } catch (Exception e) {
            return 0;
        }
    }

//    /**
//     * 单位转换
//     *
//     * @param s
//     * @return
//     */
//    private String danweizhuanhuan(String s) {
//        try {
//            String unitName=computationunitRepository.getUnitName(s);
//
//            if (StringUtils.isNotBlank(unitName)) {
//                return unitName;
//            } else {
//                return "";
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            return "";
//        }
//    }

    /**
     * split 功能封装
     */
    private String[] splitStr(String s) {
        try {
            if (StringUtils.isBlank(s)) {
                return null;
            }
            String[] str = s.split("\\,", -1);
            return str;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 字符串 转 double
     *
     * @param s
     * @return
     */
    private Double strToDouble(String s) {
        try {
            if (StringUtils.isBlank(s)) {
                return null;
            } else {
                return Double.parseDouble(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
